package com.epam.tcs.dao.hibernate.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.epam.tcs.dao.factory.LectureDAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;
import com.epam.tcs.dao.hibernate.entities.Lecture;

public class LectureDAOHibernateImpl extends ElementDAOHibernateImpl<Lecture>
		implements LectureDAO {

	public LectureDAOHibernateImpl(Class objType) {
		this.objType = objType;
	}

	@Override
	public Lecture getElement(long id) {
		Session session = ConnectionFactory.getOrInitSession();
		Lecture lecture = (Lecture) session.load(objType, (int)id);

		Hibernate.initialize(lecture.getJournalLectureStatuses());
		Hibernate.initialize(lecture.getTheme());		
				
		session.close();
		return lecture;
	}

	@Override
	public List<Lecture> getAllElements() {
		Session session = ConnectionFactory.getOrInitSession();
		List<Lecture> lectures = (List<Lecture>) session.createQuery("from " + objType.getName()).list();

		for (Lecture lecture : lectures) {			
			Hibernate.initialize(lecture.getJournalLectureStatuses());
			Hibernate.initialize(lecture.getTheme());
		}

		session.close();
		return lectures;
	}

}